package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

//执行增删改查DML语句---insert
public class JDBCDemo2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动器
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root","root");//链接数据库
            System.out.println("链接数据库成功");
            Statement statement = connection.createStatement();//创建执行sql语句的对象
            String sql = "insert into user(username,password,nickname,age) values('张三','123456','小三',18);";//插入数据
           int num= statement.executeUpdate(sql);//返回受影响的行数 1行
           if(num>0){
               System.out.println("插入成功");
           }
           connection.close();//关闭链接
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}
